﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using VCommons;
using Web.Controllers;
namespace Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            //// 注意没有参数的在上面，有参数的写在下面
            ////ＨＯＭＥ目录下隐藏,Home下面的所有视图，它没有参数
            //routes.MapRoute(
            //              "Home目录下隐藏Home",
            //               "{action}",
            //                new { controller = "home", action = "index"}
            //                );

            //带参数
            routes.MapRoute(
              "detail",
              "{controller}/{action}/{id}.shtml",/*必须是id*/
               new { controller = "Category", action = "Index", url = UrlParameter.Optional/*这个地方用url可以解决地址栏出现mvc对象的问题*/}
               );

            //不带参数
            routes.MapRoute(
                "index",
                "{controller}/{action}.shtml",/*必须是id*/
                 new { controller = "Category", action = "Index"/*这个地方用url可以解决地址栏出现mvc对象的问题*/}
                 );



            //Category下所有页面的路由，{*values}表示所有的,即Category目录下的所有操作都将失效，当然如果在它上面有过滤器，可以避免它
            //routes.MapRoute(
            //    "Category首页",
            //    "Category/{*values}",
            //    new { controller = "Category", action = "Index" }
            //    );


            //UrlParameter.Optional
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}", // URL with parameters
               new { controller = "Home", action = "Index" } // Parameter defaults
            );

            ////网站首页，如果上面路由，没有成功匹配，则指向Home下的Index
            //routes.MapRoute(
            //     "网站首页",
            //     "{*values}",
            //     new { controller = "Home", action = "index" }
            //     );




        }
        protected void Application_Error()
        {
            //VLog.VLogFactory.CreateVLog().ErrorLog(Server.GetLastError().GetBaseException().Message);
            //Response.Redirect("/Error/Index");
        }
        static System.Timers.Timer timer = new System.Timers.Timer(60000);
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
            timer.Enabled = true;
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(CreateFiles);
            VLog.VLogBase.AddVLog += new VLog.VLogBase.VLogEventHandler(ErrorController.GetLastError);//订阅日志

        }

        /// <summary>
        /// 生成静态文件（每隔1秒钟去触发Elapsed事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void CreateFiles(object sender, System.Timers.ElapsedEventArgs e)
        {
            IList<FileInfoModel> list = new StaticFileOpreate().GetFileList();
            if (list != null && list.Count > 0)
            {
                foreach (var fileInfo in list)
                {
                    CreateFile.CreateStaticFileAuto(fileInfo.RequestFile, fileInfo.ResponeFile);
                }
            }

        }
    }
}